#include "stdafx.h"
#include "GlassesParamRecognition.h"

GlassesParamRecognition::GlassesParamRecognition(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	m_pView = new QOpenglFromOpengl(this);
	//m_BuildContainer = QWidget::createWindowContainer(m_BuildView);

	ui.gridLayout_4->addWidget(m_pView, 0, 0);
	connect(&m_analy, SIGNAL(sign_ResDataArrived()), this, SLOT(DataArrived()));
	
}

void GlassesParamRecognition::on_cmdStart_clicked()
{
	m_analy.Start();
}

void GlassesParamRecognition::on_cmdStop_clicked()
{
	m_analy.Stop();

}
int GlassesParamRecognition::MatToArray(const cv::Mat& matImg, vector<unsigned char>&vData, int& iWidth, int &iHeight)
{
	int iErr = 0;
	iWidth = matImg.cols;	iHeight = matImg.rows;
	vData.resize(iWidth*iHeight * 3);
	cv::Mat_<cv::Vec3b>::const_iterator it = matImg.begin<cv::Vec3b>();
	cv::Mat_<cv::Vec3b>::const_iterator itend = matImg.end<cv::Vec3b>();

	int i = 0;
	for (; it != itend; ++it)
	{
		vData[i * 3 + 0] = (*it)[0];
		vData[i * 3 + 1] = (*it)[1];
		vData[i * 3 + 2] = (*it)[2];
		++i;
	}
	return iErr;
}
void GlassesParamRecognition::DataArrived()
{
	DataAnalysis::suResData data;
	m_analy.GetCurrentData(data);
	cv::Mat  matImg = data.m_ImgData;
	vector<unsigned char> vData;
	int iWidth, iHeight;
	 MatToArray(matImg, vData, iWidth, iHeight);
	QImage img(vData.data(), iWidth, iHeight, iWidth * 3, QImage::Format_RGB888);
	QSize size = ui.labImage->size();
	img = img.scaled(size, Qt::KeepAspectRatio);
	ui.labImage->setPixmap(QPixmap::fromImage(img));

	DataAnalysis::PPointCloudDef pClound = data.m_pHeadData;
	if (pClound.get())
	{
		QVector<QVector3D> vPnt;
		for (DataAnalysis::PCLPoint pnt : pClound->points)
		{
			vPnt.push_back(QVector3D(pnt.x, pnt.y, pnt.z));
		}
		QVector<QVector3D> vColor(vPnt.size(), QVector3D(1.0f, 1.0f, 1.0f));
		QVector<int> vInx(vPnt.size());
		for (int i = 0; i < vInx.size(); ++i)
		{
			vInx[i] = i;
		}
		static int s_name = 0;
		char buffer[128];
		sprintf_s(buffer, "mesh_%d", s_name); ++s_name;
		//m_pView->AddGeneralMesh(buffer, vPnt, QVector<QVector3D>(), vColor, vInx, GL_POINTS);


		vData.resize(data.m_Depth2D.size()*3);
		for (int i = 0; i < data.m_Depth2D.size(); ++i)
		{
			if (data.m_Depth2D[i] < 0)
			{
				vData[3 * i + 0] = 0;
				vData[3 * i + 1] = 255;
				vData[3 * i + 2] = 0;
			}
			else
			{
				unsigned char iData = data.m_Depth2D[i] % 256 ;
				vData[3 * i + 0] = iData;
				vData[3 * i + 1] = iData;
				vData[3 * i + 2] = iData;
			}
		}
		QImage img(vData.data(), data.m_Depth2D.m_iWidth, data.m_Depth2D.m_iHeight, data.m_Depth2D.m_iWidth * 3, QImage::Format_RGB888);
		QSize size = ui.labDepth->size();
		img = img.scaled(size, Qt::KeepAspectRatio);
		ui.labDepth->setPixmap(QPixmap::fromImage(img));

	}


	
}
